﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web;

namespace WJP.Context
{
    /// <summary>
    /// 分布式数据上下文。
    /// 在客户端与服务端间提供范围性的数据传输
    /// </summary>
    public static class DistributionContext
    {
        private static object _syncClientContext = new object();
        private const string _clientContextName = "ClientContext";
        private const string _globalContextName = "GlobalContext";

        /// <summary>
        /// 客户端上下文
        /// </summary>
        public static HybridDictionary ClientContext
        {
            get
            {
                HybridDictionary clientContext = DistributionContext.GetClientContext();
                if(clientContext == null)
                {
                    lock (DistributionContext._syncClientContext)
                    {
                        clientContext = DistributionContext.GetClientContext();
                        if(clientContext == null)
                        {
                            clientContext = new HybridDictionary();
                            DistributionContext.SetClientContext(clientContext);
                        }
                    }
                }

                return clientContext;
            }
        }

        /// <summary>伴随每次传输的上下文数据。</summary>
        /// <remarks>
        /// <para>
        /// 这些数据会伴随客户端到服务端、服务端到客户端的双向传输。
        /// </para>
        /// </remarks>
        public static HybridDictionary GlobalContext
        {
            get
            {
                HybridDictionary globalContext = DistributionContext.GetGlobalContext();
                if (globalContext == null)
                {
                    globalContext = new HybridDictionary();
                    DistributionContext.SetGlobalContext(globalContext);
                }
                return globalContext;
            }
        }

        /// <summary>
        /// 获取客户端上下文
        /// </summary>
        /// <returns></returns>
        internal static HybridDictionary GetClientContext()
        {
            if (HttpContext.Current != null)
                return (HybridDictionary)HttpContext.Current.Items[(object)"ClientContext"];
            
            return (HybridDictionary)Thread.GetData(Thread.GetNamedDataSlot("ClientContext"));
        }

        /// <summary>
        /// 设置客户端上下文
        /// </summary>
        /// <param name="clientContext"></param>
        internal static void SetClientContext(HybridDictionary clientContext)
        {
            if (HttpContext.Current == null)
            {
                Thread.SetData(Thread.GetNamedDataSlot("ClientContext"), clientContext);
            }
            else
                HttpContext.Current.Items["ClientContext"] = clientContext;
        }

        internal static HybridDictionary GetGlobalContext()
        {
            if (HttpContext.Current == null)
                return (HybridDictionary)Thread.GetData(Thread.GetNamedDataSlot("GlobalContext"));
            return (HybridDictionary)HttpContext.Current.Items["GlobalContext"];
        }

        internal static void SetGlobalContext(HybridDictionary globalContext)
        {
            if (HttpContext.Current == null)
                Thread.SetData(Thread.GetNamedDataSlot("GlobalContext"), globalContext);
            else
                HttpContext.Current.Items["GlobalContext"] = globalContext;
        }
        
        /// <summary>
        /// 清除上下文信息
        /// </summary>
        internal static void Clear()
        {
            DistributionContext.SetClientContext((HybridDictionary)null);
            DistributionContext.SetGlobalContext((HybridDictionary)null);
            //AppContext.Clear();
        }
    }
}
